/*******************************************************************************
* @Copyright : Shenzhen Hangshun Chip Technology R&D Co., Ltd 
* @Filename  : hk32c030xx_divsqrt.h
* @brief     : This file provides firmware functions to manage the following 
  *          functionalities of the Division and square root (DIVSQ) peripheral:
  *           + Initialization and Configuration
  *           + division operation
  *           + Square root operation
* @Author   : AE Team  
* @Version  : V1.0.1/2023-7-11
* 1.修改了divsqrt.c中HK_Dvsq_UDivsion()函数和HK_Dvsq_Divsion()
*	函数的余数的存储地址判断
* @log  V1.0.0
*
*******************************************************************************/


/* Define to prevent recursive inclusion -------------------------------------*/


#ifdef __cplusplus
 extern "C" {
#endif
 /* Includes ------------------------------------------------------------------*/
 
#include "hk32c030xx_divsqrt.h"

/** @addtogroup HK32C030xx_StdPeriph_Driver
  * @{
  */
  
 /** @defgroup DIVSQRT  DIVSQRT
  * @brief DIVSQRT driver modules
  * @{
  */

/** @defgroup DIVSQRT_Private_Functions  DIVSQRT_Private_Functions
  * @{
  */
/*******************************************************************************************************
* @function: HK_Dvsq_Init
* @brief:    Enable the DVSQ peripheral clock
********************************************************************************************************/
void HK_Dvsq_Init(void)
{ 
	RCC_AHBPeriph_DVSQ_CLK_Enable();
}

/*******************************************************************************************************
* @function: HK_Dvsq_UDivsion
* @brief:    DVSQ unsigned division operation
* @param:    u32Dividend: 32-bit unsigned dividend
* @param:    u32Divisor: divisor
* @param:    u32pRemainder: A data pointer used to hold the remainder
* @return:   quotient		
*******************************************************************************************************/
uint32_t HK_Dvsq_UDivsion(uint32_t u32Dividend, uint32_t u32Divisor, uint32_t *u32pRemainder)
{
	DVSQ_Wait();
  
	DVSQ_ConfigDivUnsigned();

	DVSQ_EnableDivFastStart();
	DVSQ->DIVIDEND = u32Dividend;
	DVSQ->DIVISOR  = u32Divisor;
	DVSQ_Wait();    

	if(u32pRemainder != NULL)
		*u32pRemainder = DVSQ->REMAINDER;

  return DVSQ->RES;
}


/*******************************************************************************************************
* @function: HK_Dvsq_Divsion
* @brief:    DVSQ signed division operation
* @param:    i32Dividend: 32-bit signed dividend
* @param:    i32Divisor: divisor
* @param:    i32pRemainder: A data pointer used to hold the remainder
* @return:   quotient		
*******************************************************************************************************/
int32_t HK_Dvsq_Divsion(int32_t i32Dividend, int32_t i32Divisor, int32_t *i32pRemainder)
{
	DVSQ_Wait();
  
	DVSQ_ConfigDivSigned();

	DVSQ_EnableDivFastStart();

	DVSQ->DIVIDEND = i32Dividend;
	DVSQ->DIVISOR  = i32Divisor;

	DVSQ_Wait();    

	if(i32pRemainder != NULL)
		*i32pRemainder = DVSQ->REMAINDER;

	return (int32_t)DVSQ->RES;
}



/*******************************************************************************************************
* @function: HK_Dvsq_Sqrt
* @brief:    The square root of DVSQ 
* @param:    u32Radicand: radicand
* @return:   The result of taking the square root			
*******************************************************************************************************/
uint32_t HK_Dvsq_Sqrt(uint32_t u32Radicand)
{
  DVSQ_Wait();
    
  /* Whether to enable high-precision square root mode */
  DVSQ_ConfigSqrtPresNormal();
  
  DVSQ->RADICAND = u32Radicand;

  DVSQ_Wait();
  
  return DVSQ->RES;
}

/*******************************************************************************************************
* @function: HK_Dvsq_Sqrt_H
* @brief:    DVSQ high-precision square root operation 
* @param:    u32Radicand: radicand
* @param:    fraction: Used to store the fractional part of the result.
* @return:   The result of taking the square root			
*******************************************************************************************************/
uint16_t HK_Dvsq_Sqrt_H(uint32_t u32Radicand, uint16_t *fraction)
{
	uint16_t result;
  DVSQ_Wait();
    
  /* Whether to enable high-precision square root mode */
  DVSQ_ConfigSqrtPresHigh();
  
  DVSQ->RADICAND = u32Radicand;

  DVSQ_Wait();
  result=(uint16_t)((DVSQ->RES>>16)&0xffff);
	*fraction = (uint16_t)(DVSQ->RES&0xffff);
	
  return result;
}

/**
  * @}
  */ 

/**
  * @}
  */

/**
  * @}
  */

